package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.FixeAreaService;
import cn.itcast.crm.domain.Customer;



@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class FixeAreaAction extends BaseAction<FixedArea>{

	/*public FixeAreaAction() {
		
	}*/
	
	@Autowired
	FixeAreaService fixeAreaService;
	
	@Action(value="fixedArea_save",results={@Result(name="success",location="/pages/base/fixed_area.html",type="redirect"),})
	public String saveFixe(){
		
		// 调用业务层
		fixeAreaService.save(model);
		System.out.println(model+"======================");
		return SUCCESS;
	}
	
	@Action(value="fixedarea_Query",results={@Result(name="success",type="json")})
	public String query(){
		
		Pageable pageable = new PageRequest(page - 1,rows);
		
		Specification<FixedArea> specification = new Specification<FixedArea>(){

			@Override
			public Predicate toPredicate(Root<FixedArea> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				
				List<Predicate> list = new ArrayList<Predicate>();
				
				// 
				if(StringUtils.isNotBlank(model.getId())){
					Predicate p1 = cb.equal(root.get("id").as(String.class), model.getId());
					list.add(p1);
				}
				
				if(StringUtils.isNotBlank(model.getCompany())){
					Predicate p2 = cb.like(root.get("company").as(String.class), "%"+model.getCompany()+"%");
					list.add(p2);
				}
				
				/*if(StringUtils.isNotBlank(model.getFixedAreaName())){
					Predicate p3 = cb.like(root.get("fixedAreaName").as(String.class), model.getFixedAreaName());
					list.add(p3);
				}*/
				
				return cb.and(list.toArray(new Predicate[0]));
			}};
		
			Page <FixedArea> page=fixeAreaService.findAll(specification,pageable);
			
			pushPageDataToValueStack(page);
		
		return SUCCESS;
		
	}

	
	// 查询未关联客户信息
	@Action(value="fixedArea_findNoAssociationCustomers",results={@Result(name="success",type="json")})
	public String findNoassociation(){
		
		// import javax.ws.rs.core.MediaType;这个包下的
		// 使用WebClient调用WebService 接口
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/service/costomerService/nosssociationcustomers")
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		
		// 将数据放入值栈
		ActionContext.getContext().getValueStack().push(collection);
		
		return SUCCESS;
	}
	
	// 查询已关联客户信息
	
	@Action(value="fixedArea_findHasAssociationFixedAreaCustomers",results={@Result(name="success",type="json")})
	public String findHasassociation(){
		
		
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/service/costomerService/associationfixedareacustomers/"
				+model.getId())
				.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	
	
	
	// 属性驱动
	// 接受客户传来的id，从select组件中传来的是一组数组，数据库区域id的匹配
	private String [] customerIds;
	
	
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}
	
	// 关联客户到定区
	@Action(value="fixedArea_associationCustomersToFixedArea",results={@Result(name="success",type="redirect",location="/pages/base/fixed_area.html")})
	public String associationCustomerToFixedArea(){
		
		String customerIdStr = StringUtils.join(customerIds,",");
		WebClient.create("http://localhost:9002/crm_management/service/costomerService"
				+ "/associationcustomerstofixedarea?customerIdStr="
				+customerIdStr+"&fixedAreaId="+model.getId()).put(null);
		return SUCCESS;
	}
	
	
	// 属性驱动接受快递员和配送标准的id,fixeArea的id已经在model里了
	private Integer courierId; // 快递员id
	private Integer takeTimeId;// 派收时间id
	
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	// 关联快递员到定区
	@Action(value="fixedArea_associationCourierToFixedArea",results={@Result(name="success",location="/pages/base/fixed_area.html",type="redirect")})
	public String associationCourierToFixedArea(){
		
		// 调用service层方法查询
		fixeAreaService.associationCourierToFixedArea(model,courierId,takeTimeId);
		
		
		return SUCCESS;
	}
	

}
